package cn.com.dycc.meeting_assist.meeting_sign.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.meeting_assist.meeting_sign.dao.SignDao;
import cn.com.dycc.meeting_assist.meeting_sign.entity.Sign;
import cn.com.dycc.meeting_assist.meeting_sign.param.FindSignParam;
import cn.com.dycc.sys_manage.deptsysmanagement.entity.DeptInfo;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserDao;
import cn.com.dycc.sys_manage.usersysmanagement.entity.UserResult;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.Paging;

/**
 * 
 * Description: 会议签到管理业务层实现
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2018年10月29日 上午10:16:03.
 * Update Date Time: 
 * @see
 */
@Service("signService")
public class SignServiceImpl implements SignService{
	
	@Resource
	private SignDao signDao;
	
	@Resource
	private UserDao userDao;
	
	@Resource(name="paging")
	private Paging paging;
	
		
	public int addSign(HttpServletRequest req, Sign sign)
			throws SignNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		String user_id = (String)session.getAttribute("id");
		if(org_id==null){
			throw new SignNotFoundException("请重新登录");
		}
		if(sign==null){
			throw new SignNotFoundException("请填写签到信息");
		}
		if(sign.getMeeting_id()==null||sign.getMeeting_id().trim().isEmpty()){
			throw new SignNotFoundException("请选择签到会议");
		}
		if(sign.getSign_flag()==null||sign.getSign_flag().trim().isEmpty()){
			throw new SignNotFoundException("请选择签到标识");
		}
		FindSignParam param_sign = new FindSignParam();
		param_sign.setMeeting_id(sign.getMeeting_id());
		param_sign.setSign_user_id(user_id);
		param_sign.setSign_flag(sign.getSign_flag());
		Sign old_sign = signDao.findSignById(param_sign);
		if(old_sign!=null){
			throw new SignNotFoundException("已经签过");
		}
		
		UserResult ur = userDao.findAllUserInfoByUserId(user_id,org_id);		
		if(ur==null){
			throw new SignNotFoundException("系统繁忙，请稍后再试");
		}
		sign.setSign_user_id(user_id);
		sign.setSign_user_name(ur.getUsername());
		DeptInfo di = ur.getDeptNo();
		if(di==null){
			throw new SignNotFoundException("系统繁忙，请稍后再试");
		}
		sign.setDept_id(di.getDept_no());		
		sign.setDept_name(di.getDept_name());
		
		sign.setOrg_id(org_id);
		sign.setSign_date(DateUtil.getNowDate(DateUtil.TIME_FMT));
		
		int n = signDao.addSign(sign);
		if(n!=1){
			throw new SignNotFoundException("系统繁忙，添加失败");
		}
		return n;
	}

	public int deleteSign(HttpServletRequest req, Sign sign) throws SignNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SignNotFoundException("请重新登录");
		}
		if(sign==null){
			throw new SignNotFoundException("请选择删除的会议室");
		}
		if(sign.getMeeting_id()==null||sign.getMeeting_id().trim().isEmpty()){
			throw new SignNotFoundException("请选择删除的签到");
		}
		if(sign.getSign_user_id()==null||sign.getSign_user_id().trim().isEmpty()){
			throw new SignNotFoundException("请选择删除的签到人");
		}
		sign.setOrg_id(org_id);
		int n = signDao.deleteSign(sign);
		if(n!=1){
			throw new SignNotFoundException("系统繁忙，删除失败");
		}
		return n;
	}


	public Sign findSignById(HttpServletRequest req, FindSignParam param)
			throws SignNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SignNotFoundException("请重新登录");
		}
		if(param==null){
			throw new SignNotFoundException("会议签到不存在");
		}
		if(param.getMeeting_id()==null||param.getMeeting_id().trim().isEmpty()){
			throw new SignNotFoundException("请选择签到");
		}
		if(param.getSign_user_id()==null||param.getSign_user_id().trim().isEmpty()){
			throw new SignNotFoundException("请选择签到人");
		}
		param.setOrg_id(org_id);
		Sign sign = signDao.findSignById(param);
		if(sign==null){
			throw new SignNotFoundException("系统繁忙，查询失败");
		}
		return sign;
	}

	public List<Sign> findSignByParam(HttpServletRequest req,FindSignParam param)
			throws SignNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SignNotFoundException("请重新登录");
		}
		Integer page = param.getPage();
		Integer limit = param.getLimit();
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}		
		param.setStart(start);	
		param.setOrg_id(org_id);
		List<Sign> list = signDao.findSignByParam(param);
		return list;
	}

	public String countSignByParam(HttpServletRequest req, FindSignParam param)
			throws SignNotFoundException {
		HttpSession session = req.getSession();
		String org_id = (String)session.getAttribute("org_id");
		if(org_id==null){
			throw new SignNotFoundException("请重新登录");
		}
		param.setOrg_id(org_id);
		String count = signDao.countSignByParam(param);
		return count;
	}

	
}
